package com.deepq.ai;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.SmUtil;
import com.deepq.util.Sm4Utils;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

/**
 * description: add a description [描述信息]
 *
 * @author : zp
 * @date 2024/4/15
 */
public class Sm4Out {
    public static String encryptFileContent(String filePath,String type) {
        StringBuilder contentBuilder = new StringBuilder();

        try (BufferedReader reader = new BufferedReader(new FileReader(filePath, StandardCharsets.UTF_8))) {
            String line;
            int i=1;
            while ((line = reader.readLine()) != null) {
                if("1".equals(type)){
                    String encryptedLine = Sm4Utils.encryptStr(line);
                    System.out.println("加密"+i+"行开始");
                    contentBuilder.append(encryptedLine).append("\n");
                    System.out.println("加密"+i+"行结束");
                }else {
                    String decryptLine = Sm4Utils.decryptStr(line);
                    System.out.println("解密"+i+"行开始");
                    contentBuilder.append(decryptLine).append("\n");
                    System.out.println("解密"+i+"行结束");
                }

                i++;
            }
            System.out.println("加、解密结束,总加、解密文件行数:"+i);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return contentBuilder.toString();
    }
    public static void main(String[] args) throws Exception {
        try {
            String data = "2023-07-12,下午三点";
            //生成key
            String key = Sm4Utils.generateKey();
            System.out.println("key:" + key);
            //加密
            String cipher = Sm4Utils.encryptEcb(key, data);
            System.out.println("加密后："+cipher);
            //判断是否正确
            System.out.println(Sm4Utils.verifyEcb(key, cipher, data));// true
            //解密
            String res = Sm4Utils.decryptEcb(key, cipher);
            System.out.println("解密后："+res);
            System.out.println(Arrays.toString("@Deepq-ai2024#$%".getBytes(StandardCharsets.UTF_8)));
            System.out.println(Sm4Utils.encryptStr("DeepQ^#@Ai_1234"));

            System.out.println(Sm4Utils.encryptStr("GFYHpoc1"));

        } catch (Exception e) {
            e.printStackTrace();
        }


//        String filePath = args[0];
//        String doType = "1";
//        if(ObjectUtil.isNotEmpty(args[1])){
//            doType = args[1];
//        }
//        if(filePath.isEmpty()){
//            System.out.println("请输入要加、解的文件路径。参数1为文件路径，参数2传入数字1或者2,1为加密，2解密");
//        }
//        encryptFileContent(filePath, doType);
    }


}
